package com.example.test01;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    Button btn;
    EditText et1;
    EditText et2;
    TextView ch;
    String HYW = "sp";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        et1 = (EditText) findViewById(R.id.name);//用户名
        et2 = (EditText) findViewById(R.id.mi) ;//密码
        btn = (Button) findViewById(R.id.but);//登录
        ch = (TextView) findViewById(R.id.choose);//选择
//        final Teacher teacher = new Teacher(MainActivity.this);
//        teacher.send();
        checkPermission();
        SharedPreferences sp = getSharedPreferences(HYW,MODE_PRIVATE);
        String username = sp.getString ("username",null);
        final String password = sp.getString("password",null);

        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)){
            et1.setText(username);
        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv =(TextView) findViewById(R.id.massage);//获取布局文件中添加的文本框组件
//                Log.d("Account", "button onclick!");
//                String name = et2.getText().toString();
//                Log.d("Password", "edit content:" + name);
                String account = et1.getText().toString();
                String pwd = et2.getText().toString();
                if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                     }
                else if(!pwd.equals(password) && !TextUtils.isEmpty(password)) {
                    Toast.makeText(MainActivity.this, "密码错误，请重输", Toast.LENGTH_LONG).show();
                }
                else {
                    SharedPreferences sp = getSharedPreferences(HYW,MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("username",account);
                    editor.putString("password",pwd);
                    editor.commit();

                    Intent intent3 = new Intent("ACTION");
                    startActivity(intent3);
//                    内部存储
//                    FileOutputStream openFileOutput(String name,int mode)throw FileNotFoundException
//                            try{
//                                FileOutputStream fos = openFileOutput("hyw.txt",MODE_PRIVATE);
//                                try {
//                                    fos.write("www.mingrisoft.com".getBytes());
//                                } catch (IOException e) {
//                                    e.printStackTrace();
//                                }
                    //外部存储
                    File root = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
                    if (root.exists()&&root.canWrite()){


                        File dir = new File(root.getAbsolutePath()+"/hywD");
                        if (!dir.exists()){
                            dir.mkdir();
                        }
                        File file = new File(dir,"hyw.txt");
                        if (!file.exists()){
                            try{
                                file.createNewFile();
                            }catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }else {
                        tv.setText("SD卡不存在或者不可写！");
                    }
                }

//               Intent intent = new Intent(MainActivity.this, DetailActivity.class);
//                startActivity(intent);
//               Intent intent2 = new Intent(Intent.ACTION_DIAL);
//                startActivity(intent2);
//                Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
//                startActivity(intent1);
            }
        });


        ch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent5 = new Intent("com.layout.choose");
                startActivity(intent5);
            }
        });
    }

    private void checkPermission(){
        int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,PERMISSION,100);
        }
    }
    private static String[] PERMISSION = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };
    @Override
    public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults){
        if (requestCode ==100){
            Log.d("hyw", "onRequestPermissionsResult: "+
            grantResults[0] + "," + grantResults[1]);
            if (grantResults[0] == PackageManager.PERMISSION_DENIED ||
                    grantResults[1] == PackageManager.PERMISSION_DENIED);
//                finish();
        }
    }
}